﻿Imports System.Windows.Forms

Public Class AddinsStatus

    Private _组件个数 As Integer = 0
    Private _组件 As New List(Of AddinsControl)
    Private _按钮 As New List(Of Button)

    Private Sub btn追加_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn添加.Click

        _组件个数 += 1
        If _组件个数 = 0 Then
            _组件个数 = 1
        End If

        Dim cl As New AddinsControl
        p1.Controls.Add(cl)

        cl.Name = "组件" & _组件个数
        cl.Left = 11
        cl.Top = 5 + 98 * (_组件个数 - 1) + 10 * (_组件个数 - 1)

        _组件.Add(cl)

        Dim btn As New Button
        p1.Controls.Add(btn)
        btn.Name = "btn删除" & _组件个数
        AddHandler btn.Click, AddressOf btn删除_Click
        _按钮.Add(btn)

        btn.Text = "删除"
        btn.Left = 645
        btn.Top = 5 + 98 * (_组件个数 - 1) + 7 * (_组件个数 - 1)
        btn.Height = 29
        btn.Tag = _组件个数

        btn添加.Top = cl.Top + cl.Height + 5

        Me.Height = btn添加.Top + btn添加.Height + 35

    End Sub

    Private Sub btn删除_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim btna As Button = CType(sender, Button)
        Dim row As Integer = btna.Tag

        _组件.RemoveAt(row - 1)
        _按钮.RemoveAt(row - 1)

        _组件个数 = -1

        Me.SuspendLayout()

        Try
            Dim cl As Windows.Forms.Control = p1.Controls("组件" & row)
            Dim c2 As Windows.Forms.Control = p1.Controls("btn删除" & row)

            cl.Visible = False
            c2.Visible = False

            p1.Controls.Remove(cl)
            p1.Controls.Remove(c2)

            If cl IsNot Nothing Then
                cl.Dispose()
            End If

            If c2 IsNot Nothing Then
                c2.Dispose()
            End If

            p1.Refresh()

        Catch ex As Exception

        End Try


        Me.Height -= 98

        刷新窗体()

        Me.Height = btn添加.Bottom + 35

        Me.ResumeLayout()
        Me.Update()

    End Sub

    Private Sub 刷新窗体()

        For index As Integer = 1 To _组件.Count

            _组件(index - 1).Left = 11
            _组件(index - 1).Top = 5 + 98 * (index - 1) + 10 * (index - 1)

            _按钮(index - 1).Left = 645
            _按钮(index - 1).Top = 5 + 98 * (index - 1) + 7 * (index - 1)
            _按钮(index - 1).Height = 29

            _按钮(index - 1).Tag = index

            If index = _组件.Count Then
                btn添加.Top = _组件(index - 1).Top + _组件(index - 1).Height + 5
            End If

        Next

        If _组件.Count = 0 Then
            btn添加.Top = 5
        End If

    End Sub

    Private Sub btn保存_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub AddinsStatus_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim lsts As List(Of 组件表) = 组件表操作类.GetAll组件(1)
        If lsts.Count > 0 Then
            For Each lst As 组件表 In lsts

                _组件个数 += 1
                If _组件个数 = 0 Then
                    _组件个数 = 1
                End If

                Dim cl As New AddinsControl
                cl.Init组件(lst)

                p1.Controls.Add(cl)

                cl.Name = "组件" & _组件个数
                cl.Left = 11
                cl.Top = 5 + 98 * (_组件个数 - 1) + 10 * (_组件个数 - 1)

                _组件.Add(cl)

                Dim btn As New Button
                p1.Controls.Add(btn)
                btn.Name = "btn删除" & _组件个数
                AddHandler btn.Click, AddressOf btn删除_Click

                If _组件个数 = 1 Then
                    btn.Visible = False
                    cl.chk不使用.Visible = False
                End If
                _按钮.Add(btn)

                btn.Text = "删除"
                btn.Left = 645
                btn.Top = 5 + 98 * (_组件个数 - 1) + 7 * (_组件个数 - 1)
                btn.Height = 29
                btn.Tag = _组件个数

                btn添加.Top = cl.Top + cl.Height + 5

                Me.Height = btn添加.Top + btn添加.Height + 35
            Next
        End If
    End Sub

    Private Sub btn保存_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn保存.Click
        If _组件.Count > 0 Then
            For index As Integer = 0 To _组件.Count - 1
                Dim 组件 As 组件表 = _组件(index).Get组件
                '组件.顺序 = index + 1
                组件表操作类.Update(组件)
            Next
        End If
        Me.Close()
    End Sub

End Class
